from plone.app.layout.viewlets import common
from zope.interface import implements, alsoProvides
from Acquisition import aq_base

from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from zope.component import getMultiAdapter
from Products.CMFCore.utils import getToolByName
from zope.viewlet.interfaces import IViewlet
from Products.Five.browser import BrowserView

from DateTime import DateTime
from Products.CMFPlone import PloneMessageFactory as _

from webcouturier.dropdownmenu.browser.dropdown import DropdownMenuViewlet

def cmp_func(x):
    return x.getObjPositionInParent()


class SiteFooterViewlet(BrowserView):
    render = ViewPageTemplateFile('footer.pt')

    implements(IViewlet)

    def __init__(self, context, request, view, manager):
        self.__parent__ = view
        self.context = context
        self.request = request
        self.view = view
        self.manager = manager

    def update(self):
        pass
    
    def navigation_root_url(self):
        portal_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_portal_state')
        return portal_state.navigation_root_url()

class GlobalSectionsViewlet(DropdownMenuViewlet):
    render = ViewPageTemplateFile('icos_sections.pt')

    
    
class SearchBoxViewlet(common.SearchBoxViewlet):
    render = ViewPageTemplateFile('icos_searchbox.pt')
    
    def navigation_root_url(self):
        return self.portal_state.navigation_root_url()
    
    def isAnon(self):
        portal_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_portal_state')
        return portal_state.anonymous()
    
class SiteActionsViewlet(common.SiteActionsViewlet):
    render = ViewPageTemplateFile('icos_site_actions.pt')
    
    
class LogoViewlet(common.LogoViewlet):
    render = ViewPageTemplateFile('icos_logo.pt')

    def navigation_root_url(self):
        return self.portal_state.navigation_root_url()

    def update(self):
        super(LogoViewlet,self).update()
        
        portal = self.portal_state.portal()
        logoName = portal.restrictedTraverse('base_properties').logoName
        self.logo_tag = portal.restrictedTraverse(logoName).tag()

        self.portal_title = self.portal_state.portal_title()
        
        self.portal_title_en = portal.getProperty('en_title', '')
        
class PathBarViewlet(common.PathBarViewlet):
    """A custom version of the path bar class 
    """
    render = ViewPageTemplateFile('icos_path_bar.pt')
    



